//
//  SubInformationViewController.m
//  CarHome
//
//  Created by lanou3g on 15/5/3.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "SubInformationViewController.h"
#import "WebView.h"
@interface SubInformationViewController ()<UIWebViewDelegate>

@property (nonatomic, retain) WebView *w;
@property (nonatomic, retain) UIView *v;
@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain) UIActivityIndicatorView *indicatorView;

- (void)loadWebView;//添加webView
- (void)addActivityIndicatorView;//添加指示器
- (void)addWebViewWithWebView:(WebView *)sender;//加载webView页面

@end

@implementation SubInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.w = [[WebView new] autorelease];
    self.view.backgroundColor = [UIColor whiteColor];
    [self addActivityIndicatorView];//指示器必须加载viewDidLoad中添加，开始，否则，指示器不会运行停止转动的方法
    [self loadWebView];
    
}

#pragma mark - 添加webView
-(void)loadWebView
{
    
    NSString *urlS = @"http://cont.app.autohome.com.cn/autov4.7/content/news/newsinfo-pm1-i870047.json";
    NSArray *a = [urlS componentsSeparatedByString:@"-i"];//以-i来截取字符串
    NSString *s = [NSString stringWithFormat:@"-i%@.json", self.Id];//准备要拼接的字符串
    
    s = [a[0] stringByAppendingString:s];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:s]];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        [_w setValuesForKeysWithDictionary:dict[@"result"]];
        
        [self addWebViewWithWebView:_w];
        
        
    }];
    
}

#pragma mark - 加载webView页面
- (void)addWebViewWithWebView:(WebView *)sender
{
    if (_w.url.length != 0) {
        
        NSString *url = _w.url;
        NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
        self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, UISCREENWIDTH, UISCREENHEIGHT)];
        [_webView loadRequest:request1];
        [self.view addSubview:_webView];
        _webView.delegate = self;
        return ;
        
    } else{
        
        [self.navigationController popToRootViewControllerAnimated:YES];
        
        UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有详情信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        
        [alerView show];
        
    }
 
}
#pragma mark - 添加指示器
-(void)addActivityIndicatorView
{
    self.v = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, UISCREENWIDTH, UISCREENHEIGHT)]autorelease];
    [self.view addSubview:_v];
   
    self.indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _indicatorView.frame = CGRectMake((UISCREENWIDTH - 150) / 2, (UISCREENHEIGHT - 150) / 2, 150, 150);
    _indicatorView.layer.masksToBounds = YES;
    _indicatorView.layer.cornerRadius = _indicatorView.bounds.size.height / 2;
    _indicatorView.backgroundColor = [UIColor grayColor];
    _indicatorView.center = CGPointMake(UISCREENWIDTH * 0.5, UISCREENHEIGHT * 0.5);
    [_indicatorView startAnimating];
    [_v addSubview:_indicatorView];
}

#pragma mark - 网页加载完成的时候调用,停止指示器
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    
    [_indicatorView stopAnimating];
    //出来的页面，上的链接不可点
    [webView stringByEvaluatingJavaScriptFromString:@"$(document).ready(function () {$(\"a\").removeAttr(\"href\");})"];
    
}





#pragma mark - 页面加载失败时，停止指示器
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [_indicatorView stopAnimating];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
